/******************************************************************************
 *   ObjectFS - Embedded Object File-System
 *
 *   Provides a full, simple no-nonsense file-system for simple embedded devices.
 *   Written by James Walmsley, james@fullfat-fs.co.uk
 *
 *   Copyright 2010 James Walmsley
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 *
 *   Removing the above notice is illegal and will invalidate this license.
 *****************************************************************************/

/*
	Endian independent memory access routines.
*/

#include "ofs_memory.h"
#include "ofs_types.h"

OFS_UINT8 OFS_getChar(OFS_UINT8 *pBuffer, OFS_INT nOffset) {
	return (OFS_UINT8) (pBuffer[nOffset]);
}

OFS_UINT16 OFS_getShort(OFS_UINT8 *pBuffer, OFS_INT nOffset) {
	OFS_UN16 u16;
	pBuffer += nOffset;

	u16.bytes.u8_1 = pBuffer[1];
	u16.bytes.u8_0 = pBuffer[0];

	return u16.u16;
}

OFS_UINT32 OFS_getLong(OFS_UINT8 *pBuffer, OFS_INT nOffset) {
	OFS_UN32 u32;
	unsigned long val;
	pBuffer += nOffset;

	u32.bytes.u8_3 = pBuffer[3];
	u32.bytes.u8_2 = pBuffer[2];
	u32.bytes.u8_1 = pBuffer[1];
	u32.bytes.u8_0 = pBuffer[0];

	val = (unsigned long) u32.u32;

	return val;
}


void OFS_putChar(OFS_UINT8 *pBuffer, OFS_INT nOffset, OFS_UINT8 Value) {
	pBuffer[nOffset] = Value;
}

void OFS_putShort(OFS_UINT8 *pBuffer, OFS_INT nOffset, OFS_UINT16 Value) {
	OFS_UN16 u16;

	u16.u16 = Value;

	pBuffer += nOffset;

	pBuffer[0] = u16.bytes.u8_0;
	pBuffer[1] = u16.bytes.u8_1;
}

void OFS_putLong(OFS_UINT8 *pBuffer, OFS_INT nOffset, OFS_UINT32 Value) {
	OFS_UN32 u32;

	u32.u32 = Value;

	pBuffer += nOffset;

	pBuffer[0] = u32.bytes.u8_0;
	pBuffer[1] = u32.bytes.u8_1;
	pBuffer[2] = u32.bytes.u8_2;
	pBuffer[3] = u32.bytes.u8_3;
}


